package com.xbongbong.formula.qlexpress.operator.date;

import com.ql.util.express.Operator;
import com.xbongbong.formula.constant.Constant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.PaasDateUtil;
import com.xbongbong.paas.toolbox.util.RegexUtil;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.BusinessRuleErrorCodeEnum;

import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2019/8/9 9:51
 * @since v1.0
 */
public class OperatorHours extends Operator {


    public OperatorHours(String name){
        this.name = name;
    }

    public OperatorHours(String aliasName, String aName, String errorInfo){
        this.aliasName = aliasName;
        this.name = aName;
        this.errorInfo = errorInfo;
    }

    /**
     * 语法：HOURS(end_time,start_time)
     * 说明：返回两个时间之间的小时数。end_time为结束时间，start_time为开始时间
     * 例如：录入【任务开始时间】、【任务结束时间】，通过HOURS函数自动填入【耗时】小时数。
     * @param list
     * list[0] 起始时间
     * list[1] 结束时间
     */
    @Override
    public Object executeInner(Object[] list) throws XbbException {
        if (Objects.isNull(list) && list.length < Constant.LENGTH) {
            throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261007);
        }
        Long startTime = PaasDateUtil.getInt(list[0]);
        Long endTime = PaasDateUtil.getInt(list[1]);
        return DateTimeUtil.betweenHours(startTime, endTime);
    }
}
